package com.dsj.gxrd.modules.activity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.activity.domain.entity.RdPeopleLedger;
import com.dsj.gxrd.modules.activity.domain.query.RdPeopleLedgerQuery;
import com.dsj.gxrd.modules.activity.mapper.RdPeopleLedgerMapper;
import com.dsj.gxrd.modules.activity.service.RdPeopleLedgerService;
import com.dsj.gxrd.modules.people.domain.entity.RdUserInfo;
import com.dsj.gxrd.modules.people.service.RdUserInfoService;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author songpin566
 * @date 2023/10/10
 **/
@Service
public class RdPeopleLedgerServiceImpl extends ServiceImpl<RdPeopleLedgerMapper, RdPeopleLedger> implements RdPeopleLedgerService {

    @Autowired
    private RdPeopleLedgerMapper rdPeopleLedgerMapper;

    @Autowired
    private RdUserInfoService userService;

    @Override
    public List<RdPeopleLedger> selectPeopleLedgerList(RdPeopleLedgerQuery query) {
        List<RdPeopleLedger> rdPeopleLedgers = rdPeopleLedgerMapper.selectPeopleLedgerList(query);
        boolean b = Integer.valueOf(3).equals(query.getType());
        for (RdPeopleLedger peopleLedger : rdPeopleLedgers) {
            String userId = peopleLedger.getUserId();
            if (StringUtils.isNotEmpty(userId)) {
                Set<Long> userIds = Arrays.stream(userId.split(",")).mapToLong(Long::parseLong).boxed().collect(Collectors.toSet());
                List<RdUserInfo> userInfos = userService.getUserInfoByIds(userIds);
                peopleLedger.setUserNames(userInfos.stream().map(RdUserInfo::getName).collect(Collectors.joining(",")));
                if (b) {
                    peopleLedger.setNowDuty(userInfos.stream().map(RdUserInfo::getNowDuty).collect(Collectors.joining(",")));
                }
            }
        }
        return rdPeopleLedgers;
    }


}
